package edu.whu.rose.clock.ssql.binding;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.whu.rose.clock.ssql.graphindex.NewGraphIndexItem;

public class NewGraphIndexItemBinding extends TupleBinding {

	public Object entryToObject(TupleInput input) {
		HashMap<Integer, HashSet<Integer>> data = new HashMap<Integer, HashSet<Integer>>();
		int c = input.readInt();
		for (int i = 0; i < c; i++) {
			int g = input.readInt();
			HashSet<Integer> v = new HashSet<Integer>();
			int cc = input.readInt();
			for (int j = 0; j < cc; j++) {
				v.add(new Integer(input.readInt()));
			}
			data.put(new Integer(g), v);
		}
		long size = input.readLong();
		return new NewGraphIndexItem(data, size);
	}

	public void objectToEntry(Object object, TupleOutput output) {
		NewGraphIndexItem ngii = (NewGraphIndexItem)object;
		output.writeInt(ngii.getData().size());
		Iterator<Integer> it1 = ngii.getData().keySet().iterator();
		while (it1.hasNext()) {
			Integer g = it1.next();
			output.writeInt(g.intValue());
			HashSet<Integer> v = ngii.getData().get(g);
			output.writeInt(v.size());
			Iterator<Integer> it2 = v.iterator();
			while (it2.hasNext()) {
				output.writeInt(it2.next().intValue());
			}
		}
		output.writeLong(ngii.getSize());
	}

}
